package com.jinsilu.freight.helper;

import android.graphics.Bitmap;
import android.text.TextUtils;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.SPUtils;
import com.hjq.http.EasyConfig;
import com.hjq.http.exception.ResultException;
import com.hjq.toast.ToastUtils;
import com.jinsilu.freight.http.model.HttpData;
import com.jinsilu.freight.http.response.UserInfoBean;
import com.jinsilu.freight.other.MTLConstants;
import com.jinsilu.freight.ui.activity.LoginActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.io.File;
import java.util.List;

/**
 * 网络货运通用工具类
 *
 * @author yangyang
 * @time 2020-11-18 11:14
 */
public class CommonHelper {

    /**
     * 存储App使用授权状态
     */
    public static void saveAppAuthStatus(boolean isAgree) {
        SPUtils.getInstance("Agreement").put("agree", isAgree);
    }

    /**
     * 获取App授权状态
     * <p>
     * 是否已授权 true 是 false 否 ；未登录默认已授权
     */
    public static boolean getAppAuthStatus() {
        return SPUtils.getInstance("Agreement").getBoolean("agree", false);
    }

    /**
     * 存储授权状态
     */
    public static void saveAuthStatus(boolean isAuth) {
        SPUtils.getInstance().put("auth", isAuth);
    }

    /**
     * 获取授权状态
     * <p>
     * 是否已授权 true 是 false 否 ；未登录默认已授权
     */
    public static boolean getAuthStatus() {
        if (isLogin())
            return SPUtils.getInstance().getBoolean("auth", false);
        return true;
    }

    /**
     * 是否登录
     *
     * @return true 登录 false 未登录
     */
    public static boolean isLogin() {
        String token = SPUtils.getInstance().getString("token");
        return !TextUtils.isEmpty(token);
    }

    /**
     * 获取TOKEN
     *
     * @return token
     */
    public static String getToken() {
        return SPUtils.getInstance().getString("token");
    }

    /**
     * 保存对象
     *
     * @param saveKey 保存键
     * @param object  保存对象
     */
    public static void saveObject(String saveKey, Object object) {
        String obj = GsonUtils.toJson(object);
        SPUtils.getInstance().put(saveKey, obj);
    }

    /**
     * 保存客服电话
     *
     * @param phone 客服电话
     */
    public static void saveCustomerPhone(String phone) {
        SPUtils.getInstance().put("CustomerPhone", phone);
    }

    /**
     * 获取客服电话
     *
     * @return 客服电话
     */
    public static String getCustomerPhone() {
        return SPUtils.getInstance().getString("CustomerPhone");
    }

    /**
     * 取出的保存对象
     *
     * @param saveKey 保存键
     */
    public static <T> Object getSavedObject(String saveKey, Class<T> object) {
        String objJson = SPUtils.getInstance().getString(saveKey);
        return GsonUtils.fromJson(objJson, object);
    }

    /**
     * 删除缓存的对象信息
     */
    public static void clearObj(String saveKey) {
        SPUtils.getInstance().remove(saveKey);
    }

    /**
     * 获取用户认证状态
     *
     * @return 认证状态 -2 未认证 0-认证失败 1-认证成功 -1-认证中
     */
    public static Integer getCertificationStatus() {
        UserInfoBean userInfoBean = (UserInfoBean) CommonHelper.getSavedObject("userInfo", UserInfoBean.class);
        if (null == userInfoBean)
            return -1;
        return userInfoBean.getCertificationStatus();
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    public static UserInfoBean getUserInfo() {
        return (UserInfoBean) CommonHelper.getSavedObject("userInfo", UserInfoBean.class);
    }

    /**
     *
     */
    public static boolean isShowBlankNote() {
        if (null == getUserInfo()) return false;
        if (TextUtils.isEmpty(getUserInfo().getPhone())) return false;
        return !getUserInfo().getPhone().equals(MTLConstants.TEST_PHONE);
    }

    /**
     * 清空缓存
     */
    public static void clearSPData() {
        SPUtils.getInstance().clear();
    }

    /**
     * 跳转至登录页
     */
    public static void goLoin() {
        EasyConfig.getInstance().getHeaders().remove("token");
        ActivityUtils.startActivity(LoginActivity.class);
        ActivityStackManager.getInstance().finishAllActivities(LoginActivity.class);
    }

    /**
     * 根据地址获取file
     *
     * @param data 地址集合
     * @return
     */
    public static File getFileFromUri(List<String> data) {
        Bitmap bitmap = ImageUtils.getBitmap(data.get(0));
        if (null != bitmap) {
            return ImageUtils.save2Album(ImageUtils.getBitmap(data.get(0)), Bitmap.CompressFormat.JPEG, 50, true);
        }
        return null;
    }

    /**
     * 取消加载状态
     *
     * @param refreshLayout 刷新控件
     */
    public static void finishedLoading(SmartRefreshLayout refreshLayout) {
        if (null != refreshLayout) {
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
        }
    }

    /**
     * 根据code 吐 不同丝
     *
     * @param e
     * @param msg 字符串资源
     */
    public static void toastErrorMsg(Exception e, String msg) {
        String errorMsg = msg;
        if (e instanceof ResultException) {
            if (510 == ((HttpData) ((ResultException) e).getData()).getCode()) {
                errorMsg = e.getMessage();
            }
        }
        ToastUtils.show(errorMsg);
    }
}
